package com.jindi.appuser.controller;

import com.jindi.appuser.common.*;
import com.jindi.appuser.dao.WinningListMapper;
import com.jindi.appuser.domain.Address;
import com.jindi.appuser.domain.Prize;
import com.jindi.appuser.domain.WinningList;
import com.jindi.appuser.service.PriceService;
import com.jindi.appuser.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * Created with IntelliJ IDEA.
 * User: duanj
 * Date: 2017-12-21
 * Time: 14:49
 * Description:
 */
@RestController
@RequestMapping("app/user")
public class UserController {

    @Autowired
    private PriceService priceService;
    @Autowired
    private UserService userService;


    @PostMapping("signin1")
    public ServerResponse signIn1(String uid){
        return userService.makeList();
    }

    /**
     * 签到
     *
     * @return
     */
    @PostMapping("signin")
    public ServerResponse signIn(String uid){
        return userService.signIn(uid);
    }

    /**
     * 检查用户名
     *
     * @return
     */
        @PostMapping("checkname")
    public ServerResponse checkname(@RequestParam(value = "phone", required = false) String phone) {
        return userService.checkUserName(phone);
    }

    //必中-金彩助手
    @PostMapping("reg")
    ServerResponse regBz(String appId, String phone, String smsCode, String pwd) {
        return userService.regBz(phone,smsCode,pwd);
    }

    //必中-金彩助手
    @PostMapping("log")
    ServerResponse logJczs(String phone,String pwd,String accessToken,String openId,String type ) {
        return userService.logJczs(phone,pwd,accessToken,openId,type);
    }

    //必中-金彩助手 type -1 注册 2-重置密码
    @PostMapping("get_sms_code")
    ServerResponse getSmsCode(String type, String phone, HttpServletRequest request) {
        String ip = getIp2(request);
        return userService.getSmsCode( phone, ip,type);
    }

    @PostMapping("rest_pwd")
    ServerResponse restPwdJczs(String appId, String phone,  String smsCode,String newPwd) {
        return userService.restPwdJczs( phone, smsCode,newPwd);
    }
    public static String getIp2(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if (StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {
            //多次反向代理后会有多个ip值，第一个ip才是真实ip
            int index = ip.indexOf(",");
            if (index != -1) {
                return ip.substring(0, index);
            } else {
                return ip;
            }
        }
        ip = request.getHeader("X-Real-IP");
        if (StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {
            return ip;
        }
        return request.getRemoteAddr();
    }
    @PostMapping("luck_draw")
    ServerResponse luckDraw( String uid) {
        return userService.luckDraw(uid);
    }

    @PostMapping("luck_draw_init")
    ServerResponse luckDrawInit( String uid) {
        return userService.luckDrawInit(uid);
    }

    @PostMapping("check_power")
    ServerResponse checkPower(String uid){
        return userService.checkPower(uid);
    }

    @PostMapping("get_user_info")
    ServerResponse getUserInfo(String uid){
        return userService.getUserInfo(uid);
    }

    @PostMapping("sign")
    ServerResponse sign(String uid){
        return userService.sign(uid);
    }


    //价格
    @PostMapping("price_list.do")
    public ServerResponse priceList(){
        return priceService.priceList();
    }

    @PostMapping("getMyPrizeList.do")
    public ServerResponse getMyPrizeList(String uid){
        if (EmptyUtil.isEmpty(uid)){
            return ServerResponse.createByErrorMessage("系统错误");
        }
        return userService.getMyPrizeList(uid);

    }

    /**
     * 添加收货地址
     * @param address
     * @return
     */
    @PostMapping("addAddress4User.do")
    public ServerResponse addAddress4User(Address address){
        if (EmptyUtil.isEmpty(address)){
            return ServerResponse.createByErrorMessage("系统错误");
        }
        return userService.addAddress4User(address);
    }

    /**
     * 查询该用户所有收货地址
     * @param uid
     * @return
     */
    @PostMapping("queryAddressListByUid.do")
    public ServerResponse queryAddressListByUid(@RequestParam(value = "uid",required = false)String uid){
        if (EmptyUtil.isEmpty(uid)){
            return ServerResponse.createByErrorMessage("系统错误");
        }
        return userService.queryAddressListByUid(uid);
    }

    /**
     * 查询该用户收货地址详情
     * @param aid
     * @return
     */
    @PostMapping("queryAddressDetailsByAid.do")
    public ServerResponse queryAddressDetailsByAid(@RequestParam(value = "aid",required = false)String aid){
        if (EmptyUtil.isEmpty(aid)){
            return ServerResponse.createByErrorMessage("系统错误");
        }
        return userService.queryAddressDetailsByAid(aid);
    }


    /**
     * 删除收货地址
     * @param aid
     * @return
     */
    @PostMapping("delAddressByAid.do")
    public ServerResponse delAddressByAid(@RequestParam(value = "aid",required = false)String aid){
        if (EmptyUtil.isEmpty(aid)){
            return ServerResponse.createByErrorMessage("系统错误");
        }
        return userService.delAddressByAid(aid);
    }

    /**
     * 修改用户使用时间
     * @param uid
     * @return
     */
    @PostMapping("updateUserTimeByUid.do")
    public ServerResponse updateUserTimeByAid(@RequestParam(value = "uid",required = false)String uid){
        if (EmptyUtil.isEmpty(uid)){
            return ServerResponse.createByErrorMessage("系统错误");
        }
        return userService.updateUserTimeByAid(uid);
    }
}
